﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Ribbon.Core.Menu;
using Graphic = Ribbon.Core.Graphics.GraphicHelper;

namespace Ribbon.Core
{
    public class RibbonMenu : RibbonPageBase
    {
        public RibbonMenu(string name)
        {
            this.Name = name;

            this.Text = name;

            Items = new RibbonMenuItemCollection(this);
        }

        public RibbonMenuItemCollection Items { get; private set; }

        Font font; 

        public override Font Font
        {
            get
            {
                return font;
            }
            set
            {
                font = value;

                if (value != null)
                {
                    foreach (RibbonMenuItem item in this.Items)
                    {
                        if (item.Font == null)
                        {
                            item.Font = this.font;
                        }
                    }
                }
            }
        }


        Ribbon parent;

        public override Ribbon Parent
        {
            get { return parent; }
            set
            {
                parent = value;

                if (this.Font == null)
                    this.Font = this.Parent.Font;

                GetSize();

                //TODO: Get Items Size
            }
        }

        public virtual void OnMouseDown(MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            { 
                Items.Location = new Point((int)this.Rect.X, (int)(this.Rect.Y + this.Rect.Height));

                if (Items.Show(Parent.Parent as Form))
                {
                    this.IsMouseHover = false;

                    this.parent.Refresh();
                }
            }
        }

        internal override void GetSize()
        {
            if (this.parent != null)
            {
                TextSize = Graphic.Graphics.MeasureString(this.Text, this.Font);

                int height = (int)(TextSize.Height + 10 * DPIHelper.DPIScale);

                if (this.parent.TabHeight < height)
                {
                    this.parent.TabHeight = height;
                }

                height = (int)(TextSize.Height + 2 * DPIHelper.DPIScale);

                float y = (this.parent.TabHeight - height) / 2f;

                if (this.Radiu > height)
                    this.Radiu = height;

                this.Rect = new Rect(this.parent.TempEmptyX, y, TextSize.Width + this.Radiu, height);

                if (this.Image != null)
                {
                    this.Rect.Width += 21f;
                }

                this.parent.TempEmptyX += this.Rect.Width + 5f;
            }
        }

        public override void OnPaint(System.Drawing.Graphics g)
        {
            if (this.Rect == null)
            {
                throw new Exception("none region");
            }

            if (this.IsMouseHover)
            {
                Brush bru_HearderBack = new SolidBrush(this.BackColor);

                if (this.Radiu > 1)
                {
                    System.Drawing.Drawing2D.GraphicsPath path = new System.Drawing.Drawing2D.GraphicsPath();

                    path.AddLine(this.Rect.X, this.Rect.Y + this.Rect.Height - this.Radiu, this.Rect.X, this.Rect.Y + this.Radiu);

                    path.AddArc(this.Rect.X, this.Rect.Y, this.Radiu, this.Radiu, 180, 90);

                    path.AddLine(this.Rect.X + this.Radiu, this.Rect.Y, this.Rect.X + this.Rect.Width - this.Radiu, this.Rect.Y);

                    path.AddArc(this.Rect.X + this.Rect.Width - this.Radiu, this.Rect.Y, this.Radiu, this.Radiu, 270, 90);

                    path.AddLine(this.Rect.X + this.Rect.Width, this.Rect.Y + this.Radiu, this.Rect.X + this.Rect.Width, this.Rect.Y + this.Rect.Height - this.Radiu);

                    path.AddArc(this.Rect.X + this.Rect.Width - this.Radiu, this.Rect.Y + this.Rect.Height - this.Radiu, this.Radiu, this.Radiu, 0, 90);

                    path.AddLine(this.Rect.X + this.Rect.Width - this.Radiu, this.Rect.Y + this.Rect.Height, this.Rect.X + this.Radiu, this.Rect.Y + this.Rect.Height);

                    path.AddArc(this.Rect.X, this.Rect.Y + this.Rect.Height - this.Radiu, this.Radiu, this.Radiu, 90, 90);

                    g.FillPath(bru_HearderBack, path);

                    using (path) { }
                }
                else
                {
                    g.FillRectangle(bru_HearderBack, this.Rect.X, this.Rect.Y, this.Rect.Width, this.Rect.Height);
                }

                using (bru_HearderBack) { }
            }

            if (this.Image != null)
            {
                int imageScale = 12;

                float totalWidth = imageScale + this.TextSize.Width + 2f;

                float tempX = this.Rect.X + (this.Rect.Width - totalWidth) / 2;

                g.DrawImage(this.Image, new Rectangle((int)(tempX) + 2, (int)(this.Rect.Y + (this.Rect.Height - imageScale) / 2 + 0.5f), imageScale, imageScale));

                using (Brush bru_Hearder = new SolidBrush(this.ForeColor))
                {
                    tempX += imageScale + 2f;

                    g.DrawString(this.Text, Font, bru_Hearder, tempX, this.Rect.Y + (this.Rect.Height - this.TextSize.Height) / 2 + 1f);
                }
            }
            else
            {
                using (Brush bru_Hearder = new SolidBrush(this.ForeColor))
                {
                    g.DrawString(this.Text, Font, bru_Hearder, this.Rect.X + (this.Rect.Width - this.TextSize.Width) / 2 + 1f, this.Rect.Y + (this.Rect.Height - this.TextSize.Height) / 2 + 1f);
                }
            }
        }
    }
}
